package com.genexususa.eo.google.data;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdApiFaultDetail;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.ApiFaultDetail;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.BatchError;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.EditorialApiFaultDetail;
import com.genexususa.eo.bing.CustomerManagementServiceClasses.ApiFault;
import com.genexususa.eo.bing.data.GxException;
import com.genexususa.eo.bing.data.GxItemException;
import com.google.api.ads.adwords.axis.v201409.cm.*;
import com.google.api.ads.adwords.axis.v201409.mcm.ManagedCustomer;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class GA_ResultItem {

    public GA_ResultItem(){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();
    }

    public GA_ResultItem(Exception e){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();

        ErrorsStruct es =  new ErrorsStruct();
        com.genexususa.eo.google.data.Error error = new com.genexususa.eo.google.data.Error();
        error.ErrorString = e.getMessage();
        error.fieldPath = e.getLocalizedMessage();
        es.Errors.add(error);
        this.HasError =  true;
        this.Errors = es;
    }

    public GA_ResultItem(ApiFault apiFault){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();


        ErrorsStruct es =  new ErrorsStruct();
        com.genexususa.eo.google.data.Error error = new com.genexususa.eo.google.data.Error();
        error.ErrorString = apiFault.getOperationErrors()[0].getMessage();
        error.fieldPath = apiFault.getFaultReason();
        es.Errors.add(error);
        this.HasError =  true;
        this.Errors = es;

    }

    public GA_ResultItem(GxException e){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();

        ErrorsStruct es =  new ErrorsStruct();

        for (GxItemException item : e.getExceptions()) {
            com.genexususa.eo.google.data.Error error = new com.genexususa.eo.google.data.Error();
            error.ErrorString = item.getMessage();
            error.fieldPath = item.getLocalizedMessage();
            es.Errors.add(error);
        }
        this.HasError =  true;
        this.Errors = es;
    }

    public GA_ResultItem(EditorialApiFaultDetail e, int index){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();

        for (int i=0; i<e.getBatchErrors().length; i++) {
            if (e.getBatchErrors()[i].getIndex() == null || (index == e.getBatchErrors()[i].getIndex() && !this.HasError)) {
                this.HasError = true;
                this.Errors = new ErrorsStruct();
                Error error = new Error();
                error.ErrorString = e.getBatchErrors()[i].getErrorCode();
                error.fieldPath = e.getBatchErrors()[i].getMessage();
                this.Errors.Errors.add(error);
            }
        }
        for (int i=0; i<e.getEditorialErrors().length; i++) {
            if (e.getEditorialErrors()[i].getIndex() == null || (index == e.getEditorialErrors()[i].getIndex() && !this.HasError)) {
                this.HasError = true;
                this.Errors = new ErrorsStruct();
                Error error = new Error();
                error.ErrorString = e.getEditorialErrors()[i].getErrorCode();
                error.fieldPath = e.getEditorialErrors()[i].getMessage();
                this.Errors.Errors.add(error);
            }
        }
        for (int i=0; i<e.getOperationErrors().length; i++) {
            if (!this.HasError) {
                this.HasError = true;
                this.Errors = new ErrorsStruct();
                Error error = new Error();
                error.ErrorString = e.getOperationErrors()[i].getErrorCode();
                error.fieldPath = e.getOperationErrors()[i].getMessage();
                this.Errors.Errors.add(error);
            }
        }
    }

    public GA_ResultItem(ApiFaultDetail e, int index){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();

        for (int i=0; i<e.getBatchErrors().length; i++) {
            if (e.getBatchErrors()[i].getIndex() == null || (index == e.getBatchErrors()[i].getIndex() && !this.HasError)) {
                this.HasError = true;
                this.Errors = new ErrorsStruct();
                com.genexususa.eo.google.data.Error error = new com.genexususa.eo.google.data.Error();
                error.ErrorString = e.getBatchErrors()[i].getErrorCode();
                error.fieldPath = e.getBatchErrors()[i].getMessage();
                this.Errors.Errors.add(error);
            }
        }


        for (int i=0; i<e.getOperationErrors().length; i++) {
            if (!this.HasError) {
                this.HasError = true;
                this.Errors = new ErrorsStruct();
                Error error = new Error();
                error.ErrorString = e.getOperationErrors()[i].getErrorCode();
                error.fieldPath = e.getOperationErrors()[i].getMessage();
                this.Errors.Errors.add(error);
            }
        }
    }

    public GA_ResultItem(AdApiFaultDetail e){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();

        ErrorsStruct es =  new ErrorsStruct();
        for(int i=0; i<e.getErrors().length; i++){
            com.genexususa.eo.google.data.Error error = new com.genexususa.eo.google.data.Error();
            error.ErrorString = e.getErrors()[i].getDetail();
            error.fieldPath = e.getErrors()[i].getMessage();
            es.Errors.add(error);
        }

        this.HasError =  true;
        this.Errors = es;
    }

    public GA_ResultItem(BatchError[] errors, int index){
        FeedAttributes = new ArrayList<GA_FeedAttribute>();
        FeedItems = new ArrayList<GA_FeedItem>();

        for (int i=0; i<errors.length; i++) {
            if (errors[i].getIndex() == null || (index == errors[i].getIndex() && !this.HasError)) {
                this.HasError = true;
                this.Errors = new ErrorsStruct();
                Error error = new Error();
                error.ErrorString = errors[i].getErrorCode();
                error.fieldPath = errors[i].getMessage();
                this.Errors.Errors.add(error);
            }
        }
    }

    public void setRegularErrorType() {
        this.ErrorType = 1;
    }

    public void setNoResponseErrorType() {
        this.ErrorType = 2;
    }

    /*@XmlElement(name = "AdGxId")
    public Long AdMobileGxId;

    @XmlElement(name = "AdId")
    public Long AdMobileId;   */

    @XmlElement(name = "AccountId")
    public Long AccountId;

    @XmlElement(name = "AccountNumber")
    public String AccountNumber;

    @XmlElement(name = "CustomerId")
    public Long CustomerId;

    @XmlElement(name = "CustomerNumber")
    public String CustomerNumber;

    @XmlElement(name = "CreateTime")
    public String CreateTime;

    @XmlElement(name = "AccountGxId")
    public Long AccountGxId;

    @XmlElement(name = "CampaignId")
    public Long CampaignId;

    @XmlElement(name = "SubCampaignGxId")
    public Long SubCampaignGxId;

    @XmlElement(name = "BudgetId")
    public Long BudgetId;

    @XmlElement(name = "BudgetGxId")
    public Long BudgetGxId;

    @XmlElement(name = "AdGroupId")
    public Long AdGroupId;

    @XmlElement(name = "AdGroupGxId")
    public Long AdGroupGxId;

    @XmlElement(name = "CriterionId")
    public Long CriterionId;

    @XmlElement(name = "CriterionGxId")
    public Long CriterionGxId;

    @XmlElement(name = "AdId")
    public Long AdId;

    @XmlElement(name = "AdGxId")
    public Long AdGxId;

   /* @XmlElement(name = "AdId")
    public Long AdTextId;



    @XmlElement(name = "AdGxId")
    public Long AdTextGxId;       */

    @XmlElement(name = "GxAccountId")
    public Long GxAccountId;

    @XmlElement(name = "AdExtensionId")
    public Long AdExtensionId;

    @XmlElement(name = "ConversionTrackerGxId")
    public Long ConversionTrackerGxId;

    @XmlElement(name = "ConversionTrackerId")
    public Long ConversionTrackerId;

    @XmlElement(name = "AdCallMetricsConversionId")
    public Long AdCallMetricsConversionId;

    @XmlElement(name = "AdCallMetricsConversionGxId")
    public Long AdCallMetricsConversionGxId;

    @XmlElement(name = "AdExtensionGxId")
    public Long AdExtensionGxId;

    @XmlElement(name = "PlatformId")
    public Long PlatformId;

    @XmlElement(name = "PlatformGxId")
    public Long PlatformGxId;

    @XmlElement(name = "LanguageId")
    public Long LanguageId;

    @XmlElement(name = "LanguageGxId")
    public Long LanguageGxId;

    @XmlElement(name = "ProximityId")
    public Long ProximityId;

    @XmlElement(name = "ProximityGxId")
    public Long ProximityGxId;

    @XmlElement(name = "TargetId")
    public Long TargetId;

    @XmlElement(name = "TargetGxId")
    public Long TargetGxId;

    @XmlElement(name = "SitelinksFeedId")
    public Long SitelinksFeedId;

    @XmlElement(name = "LinkTextFeedAttributeId")
    public Long LinkTextFeedAttributeId;

    @XmlElement(name = "LinkUrlFeedAttributeId")
    public Long LinkUrlFeedAttributeId;

    @XmlElement(name = "FeedItemId")
    public Long FeedItemId;

    @XmlElement(name = "FeedId")
    public Long FeedId;

    @XmlElement(name = "FeedName")
    public String FeedName;

    @XmlElement(name = "FeedOrigin")
    public String FeedOrigin;

    @XmlElement(name = "FeedStatus")
    public String FeedStatus;

    @XmlElement(name = "FeedTypeId")
    public int FeedTypeId;

    @XmlElement(name = "FeedGxId")
    public Long FeedGxId;

    @XmlElement(name = "BiddingGxId")
    public Long BiddingGxId;

    @XmlTransient
    public String BiddingName;

    @XmlElement(name = "BiddingId")
    public Long BiddingId;

    @XmlElement(name = "FeedMappingId")
    public Long FeedMappingId;

    @XmlElementWrapper()
    @XmlElement (name = "FeedAttribute")
    public ArrayList<GA_FeedAttribute> FeedAttributes;

    @XmlElementWrapper()
    @XmlElement (name = "FeedItem")
    public ArrayList<GA_FeedItem> FeedItems;

    @XmlTransient
    public String SubCampName;

    @XmlTransient
    public String AdGroupName;

    @XmlElement(name = "HasError")
    public boolean HasError;

    @XmlElement(name = "ErrorType")
    public int ErrorType;
    // ErrorType 1: Regular Error
    // ErrorType 2: No Response

    @XmlElement (name = "Errors")
    public ErrorsStruct Errors;

    @XmlTransient
    public LocationExtension AdExtensionLocation;

    /*@XmlTransient
    public MobileExtension MobileExtension;    */

   /* @XmlTransient
    public SitelinksExtension AdExtensionSitelinks;  */

    @XmlTransient
    public BiddableAdGroupCriterion BiddableCriterion;

    @XmlTransient
    public NegativeAdGroupCriterion NegativeCriterion;

    @XmlTransient
    public AdGroupAd TextAd;

    @XmlTransient
    public AdGroupAd ImageAd;

    @XmlTransient
    public AdGroupAd MobileImageAd;

    @XmlTransient
    public AdGroupAd MobileAd;


    @XmlTransient
    public NegativeCampaignCriterion NegativeCampaign;

    @XmlTransient
    public Criterion Language;

    @XmlTransient
    public Criterion Platform;

    @XmlTransient
    public ManagedCustomer Customer;

    @XmlTransient
    public AdWordsConversionTracker ConversionTracker;

    @XmlTransient
    public AdCallMetricsConversion AdCallMetricsConversion;

    /*@XmlTransient
    public InAppLinkExtension InAppExtension;  */

    @XmlTransient
    public Criterion Proximity;

    @XmlTransient
    public CampaignCriterion CampaignCriterion;

    @XmlTransient
    public Feed Feed;

    @XmlTransient
    public FeedItem FeedItem;

    @XmlTransient
    public FeedMapping FeedMapping;

    @XmlTransient
    public SharedBiddingStrategy BiddingStrategy;

}
